﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;

using RaceList.activity.help;
using RaceList.activity.util;

namespace RaceList.activity.mhalloween
{
    /// <summary>
    ///     这个页面是一个小页面，供ajax读取排行
    /// </summary>
    public partial class Paihb : Page
    {
        private int _pageNo;
        private int _pageSize;
        private int _userid;

        /// <summary>
        ///     在后台就决定了所有人的样式
        /// </summary>
        public List<Paihang> Paihangs { get; set; }

        public int PageSize
        {
            get { return _pageSize; }
        }

        public int PageNo
        {
            get { return _pageNo; }
        }

        public int UserId
        {
            get { return _userid; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //加个参数就能做成个人排行榜了_userid

            _pageSize = 10;
            _pageNo = 1;
            try
            {
                _pageSize = Convert.ToInt32(Request[Parameters.pagesize]);
                _pageNo = Convert.ToInt32(Request[Parameters.pageno]);
                _userid = Convert.ToInt32(Request[Parameters.userId]);
            }
            catch (Exception)
            {
            }
            if (!IsPostBack)
            {
                InitPaiHangs(_pageNo, _pageSize);
            }
        }
        /// <summary>
        /// 排行的算法太混乱了吧，如果相同成绩的怎么算呢
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        public void InitPaiHangs(int pageNo, int pageSize)
        {
            Paihangs = new List<Paihang>();
            int skipNum = (pageNo - 1) * pageSize;
            int takeNum = pageSize;
            if (_userid == 0)
            {
                #region 获取总排行榜

                //先获取3*10条记录，然后剔除2*10条记录，然后获取最后的10条记录
                List<Activity_Relay> list =
                    RelayQuery.GetPaihang(pageNo * pageSize).Skip(skipNum).Take(takeNum).ToList();
                foreach (Activity_Relay item in list)
                {
                    Paihangs.Add(new Paihang
                    {
                        Headimgurl = item.WeixinUser.Headimgurl,
                        Nickname = item.WeixinUser.Nickname,
                        SumScore = item.SumScore,
                        RelayId = item.Id,
                        CreateDate = item.AddTime
                    });
                }

                #endregion
            }
            else
            {
                #region 获取个人成绩排行榜 ，但是有一个问题就是个人排行榜提供的东西肯定和这个不一样，前台也加一个判断

                var list = RelayQuery.GetRelayList(_userid)
                    .Where(s => s.SumScore != (double)0)
                    .OrderByDescending(s => s.AddTime)
                    .Skip(skipNum).Take(takeNum);
                foreach (Activity_Relay item in list)
                {
                    var paihang = new Paihang
                    {
                        SumScore = item.SumScore,
                        RelayId = item.Id,
                        IsGameOver = item.IsGameOver,
                        CreateDate = item.AddTime
                    };
                    Paihangs.Add(paihang);
                }

                #endregion
            }
        }
    }
}